package com.pet.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pet.constant.AIConstant;
import com.pet.context.BaseContext;
import com.pet.controller.AI.aiUtil.ConnectionUtil;
import com.pet.controller.AI.sparkAI.SparkAi;
import com.pet.dto.web.PetHealthInformationDTO;
import com.pet.entity.AI.AiContent;
import com.pet.entity.PetHealthInformation;
import com.pet.mapper.PetHealthInformationMapper;
import com.pet.mapper.UserInformationMapper;
import com.pet.pageDto.PetHealthPageDTO;
import com.pet.result.PageResult;
import com.pet.result.Result;
import com.pet.service.PetHealthInformationService;
import com.pet.exchange.PetHealthUtil;
import com.pet.vo.web.InvitationInformationVO;
import com.pet.vo.web.PetHealthInformationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PetHealthInformationServiceImpl implements PetHealthInformationService {

    @Autowired
    private PetHealthInformationMapper petHealthInformationMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;
    @Autowired
    private PetHealthUtil petHealthUtil;
    @Autowired
    private ConnectionUtil connectionUtil;

    /**
     * 根据用户的id进行查询
     * 查询宠物健康信息
     *
     * @return
     */
    public List<PetHealthInformationVO> selectPetHealthInformation() {
        //获取实体 根据用户的id进行查询
        List<PetHealthInformation> entityList =
                petHealthInformationMapper.selectPetHealthInformation(BaseContext.getCurrentId());
        log.info("selectPetHealthInformation:{}",entityList);
        List<PetHealthInformationVO> petHealthInformationVOList = new ArrayList<>();
        for (PetHealthInformation entity : entityList) {
            //创建vo
            PetHealthInformationVO vo = new PetHealthInformationVO();
            //赋值
            BeanUtils.copyProperties(entity, vo);

            vo.setCreateUser(userInformationMapper.selectName(entity.getCreateUser()));
            vo = petHealthUtil.petHealthInfoExchange(entity);
            //设置用户名
            vo.setCreateUser(userInformationMapper.selectName(Math.toIntExact(BaseContext.getCurrentId())));
            //调用工具类
            vo = petHealthUtil.PetHealth(entity, vo);
            petHealthInformationVOList.add(vo);
        }
        return petHealthInformationVOList;
    }

    /**
     * 添加宠物健康信息
     * @param dto
     */
    public void insertPetHealthInformation(PetHealthInformationDTO dto) {
        log.info("dto:{}",dto);
        //创建实体
        PetHealthInformation entity = new PetHealthInformation();
        //赋值
        BeanUtils.copyProperties(dto, entity);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));

        //操作数据库
        log.info("{}",entity);
        petHealthInformationMapper.insertPetHealthInformation(entity);
    }

    /**
     * 修改(更新)宠物健康信息
     * @param dto
     */
    public void updatePetHealthInformation(PetHealthInformationDTO dto) {
        //操作数据库
        petHealthInformationMapper.updatePetHealthInformation(dto);
    }

    /**
     * 删除宠物健康信息
     * @param id
     */
    public void deletePetHealthInformation(Integer id) {
        //操作数据库
        petHealthInformationMapper.deletePetHealthInformation(id);
    }

    /**
     * 管理端
     * 分页查询所有宠物健康信息
     * @param dto
     * @return
     */
    public PageResult pageQuery(PetHealthPageDTO dto) {
        //设置参数
        Page<PetHealthInformationVO> page = new Page<>();
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //try 传入dto中的数据调用mapper进行分页查询
        try (Page<PetHealthInformation> petHealthInformationList = petHealthInformationMapper.pageQuery(dto)){
            //遍历list 调用petHealthInfoUtil对数据进行修改
            for (PetHealthInformation petHealthInformation : petHealthInformationList) {
                PetHealthInformationVO petHealthInformationVO = petHealthUtil.petHealthInfoExchange(petHealthInformation);
                page.add(petHealthInformationVO);
            }
        }
        //将返回的数据进行修改
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询指定宠物健康信息AI
     */
    public String selectPetHealthInformationAI(Integer id ) {
        //根据id查询健康信息
        PetHealthInformation petHealthInformation = petHealthInformationMapper.selectPetHealthInformationById(id);
        //调用ai问询接口，传入健康信息
        AiContent aiContent = new AiContent();
        aiContent.setContent("给我宠物健康建议");
        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", AIConstant.headPart+ aiContent.getContent()+
                AIConstant.answerFormat+AIConstant.petHealthAnswerFormat+"参考数据："+petHealthInformation+"参考注释"+AIConstant.petHealthInfo);

        Result result = connectionUtil.configDialogueInfo(message);
        return result.getData().toString();
    }
}
